package com.zhoujian.Zookeeper;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;

/**
 * @author zhoujian
 * @version 1.0
 * @date 2023/9/23 9:28
 */

//只编写自己关心的事件，只判断一次
    //有这么多方法
public class MyWatcher implements Watcher {
    @Override
    public void process(WatchedEvent event) {
        //判断事件类型，是否是连接类型的事件
        if (event.getType() == Event.EventType.None){
            if (event.getState() == Event.KeeperState.SyncConnected){
                System.out.println("zookeeper连接成功");
            } else if (event.getState() == Event.KeeperState.AuthFailed) {//认证失败
                System.out.println("zookeeper认证失败");
            } else if (event.getState() == Event.KeeperState.Disconnected) {//断开连接
                System.out.println("zookeeper断开连接");
            }
        } else if (event.getType() == Event.EventType.NodeCreated) {
            System.out.println(event.getPath() + "被创建了");
        } else if (event.getType() == Event.EventType.NodeDeleted) {
            System.out.println(event.getPath() + "被删除了");
        }else if (event.getType() == Event.EventType.NodeDataChanged) {
            System.out.println(event.getPath() + "节点的数据被改变了");
        }else if (event.getType() == Event.EventType.NodeChildrenChanged) {
            System.out.println(event.getPath() + "子节点发生了变化");
        }
    }
}
